package yaml

import (
	"admin/src/log"
	"os"

	"gopkg.in/yaml.v3"
)

type Config struct {
	App   App   `yaml:"app"`
	Log   Log   `yaml:"log"`
	Mysql Mysql `yaml:"mysql"`
}

type App struct {
	Name    string `yaml:"name"`
	Version string `yaml:"version"`
	Env     string `yaml:"env"`
}
type Mysql struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Database string `yaml:"database"`
}
type Log struct {
	Level  string `yaml:"level"`
	Format string `yaml:"format"`
	Output string `yaml:"output"`
	File   struct {
		Path    string `yaml:"path"`
		MaxSize int    `yaml:"max_size"`
		MaxAge  int    `yaml:"max_age"`
	} `yaml:"file"`
}

func LoadConfig() *Config {
	data, err := os.ReadFile("src/yaml/config.yaml")
	if err != nil {
		log.Logger.Fatal("读取yaml文件失败")
	}
	var config Config
	err = yaml.Unmarshal(data, &config)
	if err != nil {
		log.Logger.Fatal("解析yaml文件失败")
	}
	return &config
}
